﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Base;

namespace HRM
{
    public partial class FormBankInfo : FormBaseData
    {
        private Ctrl_General mCtrlGeneral;
        private DataSet mdsBank;
        private SqlDataAdapter mdaBankUpdate;

        public FormBankInfo()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
        }

        protected override void BaseForm_Activated(object sender, System.EventArgs e)
        {
            base.BaseForm_Activated(sender, e);
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        protected override void BasicForm_Closing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            base.BasicForm_Closing(sender, e);
        }

        protected override void LoadMasterData()
        {
            try
            {
                btnCancel = btncancel;
                btnOk = btnok;
                lblStatus = lblstatus;
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot open Banking Information'. Error: " + ex.Message, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        #region LoadData
        private void LoadData()
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                ClearDataBinding(this);
                if (mCtrlGeneral == null) mCtrlGeneral = new Ctrl_General();

                string ls_SQL = " Select ID_Bank, Bank_Name, Bank_Address, CreatedUser, UpdatedUser, CreatedDate, UpdatedDate From HRM_Bank ";

                mdsBank = mCtrlGeneral.GetSelect(ls_SQL, "HRM_Bank");
                mCtrlGeneral.GetAllData(out mdaBankUpdate, "HRM_Bank", "");

                if (mdsBank == null) return;
                //Binding: TextBox, Label
                txtBankCode.DataBindings.Add(new Binding("Text", mdsBank.Tables[0], "ID_Bank"));
                txtBankName.DataBindings.Add(new Binding("Text", mdsBank.Tables[0], "Bank_Name"));
                txtBankAddress.DataBindings.Add(new Binding("Text", mdsBank.Tables[0], "Bank_Address"));

                mRowManager = (CurrencyManager)this.BindingContext[mdsBank.Tables[0]];
                // How to refresh the status of the row position?Ordinal_Number
                mRowManager.PositionChanged += new EventHandler(RowMaster_Changed);

                SetData2List(mdsBank);
                SetReadOnly(this, false);
                base.LoadMasterData();
                Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot load Banking Information data'. Error: " + ex.Message, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Cursor = Cursors.Default;
            }
        }
        #endregion

        #region SetReadOnly
        private void SetReadOnly(Control aCtrl, bool abReadOnly)
        {
            foreach (Control lCtrl in aCtrl.Controls)
            {
                switch (lCtrl.GetType().ToString())
                {
                    case "System.Windows.Forms.TextBox":
                        ((TextBox)lCtrl).ReadOnly = !abReadOnly;
                        break;
                }
            }
        }
        #endregion

        #region add
        public override void add()
        {
            try
            {
                base.add();
                mRowManager.EndCurrentEdit();
                mRowManager.AddNew();
                SetReadOnly(this, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add new Banking Information  Error: " + ex.Message, ConstantKeys.Title);
            }
        }
        #endregion

        #region modify
        public override void modify()
        {
            base.modify();
            SetReadOnly(this, true);
            txtBankCode.ReadOnly = true;
        }
        #endregion

        #region cancel
        public override void cancel()
        {
            if (mdsBank == null) return;
            mRowManager.EndCurrentEdit();
            mdsBank.RejectChanges();
            base.cancel();
            SetReadOnly(this, false);
        }
        #endregion

        #region save
        public override void save()
        {
            mRowManager.EndCurrentEdit();
            if (mdsBank == null) return;
            if (txtBankCode.Text.Trim().Equals(""))
            {
                MessageBox.Show("Cannot save Banking Information data'. Error: Bank Code. cannot empty.", ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtBankCode.Focus();
                return;
            }

            DataRow dr = mdsBank.Tables[0].Rows[mRowManager.Position];
            if (dr.RowState == DataRowState.Modified)
            {
                dr["UpdatedUser"] = mMainForm.ms_UserID;
                dr["UpdatedDate"] = DateTime.Now;
            }
            else if (dr.RowState == DataRowState.Added)
            {
                dr["CreatedUser"] = mMainForm.ms_UserID;
                dr["CreatedDate"] = DateTime.Now;
            }

            string ls_ColName = "", ls_TableName = "";
            string ls_Error = mCtrlGeneral.Update(mdaBankUpdate, mdsBank, "HRM_Bank", out ls_ColName, out ls_TableName);
            if (ls_Error == "")
            {
                mdsBank.AcceptChanges();
                base.save();
                SetReadOnly(this, false);
            }
            else
                MessageBox.Show("Cannot save Banking Information data'. Error: " + ls_Error, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        #endregion

        #region delete
        public override void delete()
        {
            base.delete();
            if (!mb_AcceptDelete) return;

            DataSet ldsDelete = mdsBank;
            DataRow ldrDelete = ldsDelete.Tables[0].Rows[mRowManager.Position];
            int li_Pos = mRowManager.Position;
            ldrDelete.Delete();
            string lsColName = "", lsTableName = "";
            string lsReturnMessage = mCtrlGeneral.Update(mdaBankUpdate, ldsDelete, "HRM_Bank", out lsColName, out lsTableName);
            if (lsReturnMessage != "")
            {
                MessageBox.Show("Cannot delete Banking Information data. Error: " + lsReturnMessage, ConstantKeys.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ldsDelete.RejectChanges();
                ldsDelete.AcceptChanges();
                mRowManager.Position = li_Pos;
                SetReadOnly(this, false);
                return;
            }

            base.post_delete();

            SetReadOnly(this, false);
        }
        #endregion

        private void btnok_Click(object sender, System.EventArgs e)
        {
            save();
        }

        private void btncancel_Click(object sender, System.EventArgs e)
        {
            cancel();
        }

    }
}
